package org.nudt.video.controller

import org.nudt.common.model.PageData
import org.nudt.common.model.ResponseData
import org.nudt.common.model.Type
import org.nudt.common.model.Video
import org.nudt.common.model.vo.VideoVO
import org.nudt.video.service.VideoService
import org.springframework.data.domain.PageRequest
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/v1/video")
class VideoController(private val videoService: VideoService) {
    
    /**
     * 获取所有视频分类列表
     */
    @GetMapping("/type")
    fun getTypeList(): ResponseData<MutableList<Type>> {
        return ResponseData.success(videoService.getVideoTypeList())
    }
    
    /**
     * 根据分类获取视频列表
     * @param videoClass 视频大类
     */
    @GetMapping("/list/{videoClass}")
    fun videoListByVideoClass(
        @PathVariable videoClass: Int,
        @RequestParam(required = false, defaultValue = "1") page: Int,
        @RequestParam(required = false, defaultValue = "10") size: Int
    ): ResponseData<PageData<VideoVO?>> {
        return ResponseData.success(videoService.getVideoListByVideoClass(videoClass, PageRequest.of(page - 1, size)))
    }
    
    /**
     * 根据筛选条件获取视频列表
     * @param type 视频大类
     * @param area 视频地区
     * @param year 视频年份
     */
    @GetMapping("/{type}/filter")
    fun filterVideoList(
        @PathVariable type: Int,
        @RequestParam(required = false) area: String?,
        @RequestParam(required = false) year: Int?,
        @RequestParam(required = false, defaultValue = "2020") minYear: Int,
        @RequestParam(required = false, defaultValue = "1") page: Int,
        @RequestParam(required = false, defaultValue = "10") size: Int
    ): ResponseData<PageData<VideoVO>> {
        return ResponseData.success(videoService.filterVideoList(type, area, year, minYear, PageRequest.of(page - 1, size)))
    }
    
    /**
     * 搜索视频
     * @param keyword 关键词
     */
    @GetMapping("/search")
    fun searchVideo(
        @RequestParam keyword: String,
        @RequestParam(required = false, defaultValue = "1") page: Int,
        @RequestParam(required = false, defaultValue = "10") size: Int
    ): ResponseData<PageData<VideoVO>> {
        return ResponseData.success(videoService.searchVideoList(keyword, PageRequest.of(page - 1, size)))
    }
    
    /**
     * 根据视频id获取视频详情
     * @param id 视频id
     */
    @GetMapping("/{id}")
    fun videoDetail(
        @PathVariable id: Int,
        @RequestParam appid: String,
    ): ResponseData<Video> {
        return ResponseData.success(videoService.getVideoById(id, appid))
    }
    
    /**
     * 根据分类获取推荐视频列表
     * @param type 视频子类
     */
    @GetMapping("/{type}/recommend")
    fun videoListRecommendByType(
        @PathVariable type: Int,
        @RequestParam(required = false, defaultValue = "10") size: Int
    ): ResponseData<List<VideoVO>> {
        return ResponseData.success(videoService.getVideoListRecommendByType(type, PageRequest.of(0, size)))
    }
    
    /**
     * 发布指定视频的错误信息
     * @param id 视频id
     */
    @PostMapping("/{id}/report")
    fun reportVideoError(@PathVariable id: Int) {
    
    }
    
    /**
     * 获取指定推荐栏视频列表
     * @param banner 推荐栏id
     */
    @GetMapping("/banner/{banner}")
    fun bannerVideoList(@PathVariable banner: Int): ResponseData<List<VideoVO>> {
        return ResponseData.success(videoService.getBannerVideoList())
    }
    
}